package com.sc.nft.entity.vo;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.sc.nft.entity.EcologyPool;
import com.sc.nft.entity.UserAssets;
import com.sc.nft.entity.UserCollection;
import com.sc.nft.entity.UserEquityProps;
import com.sc.nft.entity.vo.ecology.EcologyCastGasVO;
import com.sc.nft.enums.BoxConditionEnum;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;

@Data
public class QueryWebBoxConditionPackagingVO {
    @ApiModelProperty(value = "盲盒nft持有配置")
    private QueryWebBoxConditionCollectionVO queryBoxConditionCollection;

    @ApiModelProperty(value = "兑换配置")
    private List<QueryWebBoxConditionVO> queryBoxCondition;
    
    @ApiModelProperty(value = "上链费消耗")
    private EcologyCastGasVO ecologyCastGas;


    /**
     * 获取结果集中的藏品结果,一般来说一个列表里面只会有一个.
     *
     * @return
     */
    @JsonIgnore
    public QueryWebBoxConditionVO getCollectionResult() {
        List<QueryWebBoxConditionVO> collect = this.queryBoxCondition.stream().filter(queryWebBoxConditionVO -> queryWebBoxConditionVO.getType().equals(1)).collect(Collectors.toList());
        if (CollUtil.isNotEmpty(collect)) {
            return collect.get(0);
        }
        return null;
    }

    /**
     * NFT的配置转换
     *
     * @param userCollections
     */
    @JsonIgnore
    public void nftConfiguration(List<UserCollection> userCollections) {
        // 获取nft数据.
        List<QueryWebBoxConditionVO> collect = this.queryBoxCondition.stream().filter(queryWebBoxConditionVO -> queryWebBoxConditionVO.getType().equals(1)).collect(Collectors.toList());
        QueryWebBoxConditionCollectionVO queryWebBoxConditionCollectionVO = new QueryWebBoxConditionCollectionVO();
        if (CollUtil.isNotEmpty(collect)) {
            queryWebBoxConditionCollectionVO.setName(collect.get(0).getName());
            queryWebBoxConditionCollectionVO.setCreationContent(collect.get(0).getCreationContent());
            if (CollUtil.isNotEmpty(userCollections)) {
                queryWebBoxConditionCollectionVO.setIsHold(Boolean.TRUE);
            } else {
                queryWebBoxConditionCollectionVO.setIsHold(Boolean.FALSE);
            }
            this.queryBoxConditionCollection = queryWebBoxConditionCollectionVO;
        }
    }

    public void setQueryBoxCondition(List<QueryWebBoxConditionVO> queryBoxCondition) {
//        List<QueryWebBoxConditionVO> collect = queryBoxCondition.stream().filter(queryWebBoxConditionVO -> queryWebBoxConditionVO.getType().equals(1)).collect(Collectors.toList());
//        if (CollUtil.isNotEmpty(collect)) {
//            QueryWebBoxConditionCollectionVO queryWebBoxConditionCollectionVO=new QueryWebBoxConditionCollectionVO();
//            queryWebBoxConditionCollectionVO.setName(collect.get(0).getName());
//            queryWebBoxConditionCollectionVO.setCreationContent(collect.get(0).getCreationContent());
//            queryWebBoxConditionCollectionVO.setIsHold();
//            this.queryBoxConditionCollection=queryWebBoxConditionCollectionVO;
//        }
        this.queryBoxCondition = queryBoxCondition;
    }

    /**
     * 获取兑换所需要的权益道具表id集合
     *
     * @return
     */
    @JsonIgnore
    public List<Long> getEquityPropsIds() {
        return this.queryBoxCondition.stream().filter(queryWebBoxConditionVO -> queryWebBoxConditionVO.getType().equals(2)).map(QueryWebBoxConditionVO::getTripartiteId).collect(Collectors.toList());
    }


    /**
     * 处理权益道具的分配
     *
     * @param userEquityProps
     */
    @JsonIgnore
    public void handleTheAllocationOfEquityProps(List<UserEquityProps> userEquityProps) {
        this.queryBoxCondition.stream().filter(queryWebBoxConditionVO -> queryWebBoxConditionVO.getType().equals(2) && queryWebBoxConditionVO.getConversionType().equals(BoxConditionEnum.EQUITY_PROPS)).forEach(queryWebBoxConditionVO -> {
            List<UserEquityProps> collect = userEquityProps.stream().filter(result -> result.getEquityPropsId().equals(queryWebBoxConditionVO.getTripartiteId())).collect(Collectors.toList());
            if (CollUtil.isNotEmpty(collect)) {
                if (collect.size() > 0) {
                    queryWebBoxConditionVO.setUserOwned(Convert.toStr(Convert.toBigDecimal(collect.size())));
                } else {
                    queryWebBoxConditionVO.setUserOwned("0");
                }
            } else {
                queryWebBoxConditionVO.setUserOwned("0");
            }
        });
    }

    @JsonIgnore
    public void planetFragmentsResultInConversion(UserAssets userAssets) {
        if (CollUtil.isEmpty(this.queryBoxCondition)) {
            return;
        }
        this.queryBoxCondition.stream().filter(queryWebBoxConditionVO -> queryWebBoxConditionVO.getType().equals(2) && queryWebBoxConditionVO.getConversionType().equals(BoxConditionEnum.STAR_FRAGMENT)).forEach(queryWebBoxConditionVO -> {
            queryWebBoxConditionVO.setCostQuantity(Convert.toStr(Convert.toBigDecimal(queryWebBoxConditionVO.getCostQuantity()).setScale(2, BigDecimal.ROUND_DOWN)));
            if (BeanUtil.isNotEmpty(userAssets)) {
                queryWebBoxConditionVO.setUserOwned(Convert.toStr(userAssets.getTokenNum().setScale(2, BigDecimal.ROUND_DOWN)));
            }
        });
    }

    @JsonIgnore
    public void handleEcologyPoolConvert(EcologyPool ecologyPool) {
        if (CollUtil.isEmpty(this.queryBoxCondition)) {
            return;
        }
        this.queryBoxCondition.stream().filter(queryWebBoxConditionVO -> queryWebBoxConditionVO.getType().equals(2) && queryWebBoxConditionVO.getConversionType().equals(BoxConditionEnum.ECOLOGY_POOL_POINTS)).forEach(queryWebBoxConditionVO -> {
            queryWebBoxConditionVO.setCostQuantity(Convert.toStr(Convert.toBigDecimal(queryWebBoxConditionVO.getCostQuantity()).setScale(2, BigDecimal.ROUND_DOWN)));
            queryWebBoxConditionVO.setUserOwned("0.00");
            if (BeanUtil.isNotEmpty(ecologyPool)) {
                queryWebBoxConditionVO.setUserOwned(Convert.toStr(ecologyPool.getLinkCount().setScale(2, BigDecimal.ROUND_DOWN)));
            }
        });
    }

    public void removeNftData() {
        this.queryBoxCondition = this.queryBoxCondition.stream().filter(queryWebBoxConditionVO -> !queryWebBoxConditionVO.getType().equals(1)).collect(Collectors.toList());
    }
}
